package fhb.mobile.manager;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import fhb.mobile.manager.localDB.LocalDBManager;
import fhb.mobile.manager.remoteDB.RemoteDBManager;
import fhb.mobile.todoapp.LoginActivity;
import fhb.mobile.todoapp.ToDoActivity;
import fhb.mobile.todoapp.datamodell.ToDoItem;
import fhb.mobile.todoapp.datamodell.ToDoItemList;

public class Storage {

	protected static ToDoItemList todoList = new ToDoItemList();
	protected static boolean isLogin = false;
	protected static Context context;
	protected static LocalDBManager localDB = new LocalDBManager();
	protected static RemoteDBManager remoteDB = new RemoteDBManager();
	protected static String username;
	protected static String password;
	protected static boolean sort = true;

	public static void switchSort() {
		sort = !sort;
		readLocalDB();
	}
	
	

	public static Context getContext() {
		return context;
	}



	public static void setContext(Context context) {
		Storage.context = context;
	}



	public static ToDoItemList getToDoItemList() {
		return todoList;
	}

	public static boolean loginApp(String _username, String _password,
			Context _context) {
		context = _context;
		username = _username;
		password = _password;
		login(username, password, context);
		if (isLogin) {
			readLocalDB();
		}
		return isLogin;
	}

	public static void insertToDo(ToDoItem todo) {
		localDB.addToDo(todo);
		readLocalDB();
	}

	public static void removeToDo(ToDoItem todo) {
		localDB.deleteToDo(todo);
		remoteDB.deleteToDo(todo);
		readLocalDB();
	}

	public static void updateToDo(ToDoItem todo) {
		localDB.updateToDo(todo);
		readLocalDB();
	}

	public static ToDoItem getToDo(long id) {
		for (ToDoItem todo : todoList) {
			if (id == todo.getCreateDate()) {
				return todo;
			}
		}
		return null;
	}

	protected static void login(String username, String password,
			Context context) {
		// context = this der activity
		isLogin = localDB.login(username, password, context);
		remoteDB.loginDB(username, password);
	}

	protected static void readLocalDB() {
		todoList = localDB.getAllToDo(sort);
	}

	public static void syncDB() {
		try {
			if (isLogin)
				remoteDB.syncDB(localDB.getAllToDo(sort));
		} catch (Exception e) {
			Toast.makeText(context, "Sync failes", 3000).show();
		}
		Toast.makeText(context, "Sync success", 3000).show();
	}

	public static boolean isLogin() {
		return isLogin;
	}

	public static void setLogin(String isLogin) {
		if (isLogin.equals("false")) {
			Intent intent = new Intent(context, LoginActivity.class);
			context.startActivity(intent);
		} 
		if (isLogin.equals("true")){
			if (Storage.isLogin == false) {
				localDB.addAccount(username, password);
				Intent intent = new Intent(context, ToDoActivity.class);
				readLocalDB();
				syncDB();
				context.startActivity(intent);
			} else {
				syncDB();
			}
		}

	}

	public static String getUsername() {
		return username;
	}

	public static void setUsername(String username) {
		Storage.username = username;
	}

}
